from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
TIMEOUT=10
class Base():
    # 初始化驱动
    def __init__(self,driver: WebDriver):
        self.driver=driver
    # 打开浏览器
    def goto(self,url):
        self.driver.get(url)
    # 退出浏览器
    def quit(self):
        self.driver.quit()

    # 定位方法
    def find_element(self,loc):
            # 异常
            try:
                # 显式等待
                WebDriverWait(self.driver, 20, 0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
                return self.driver.find_element(*loc)
            except Exception as e:
                print(e)

    def wait_time(self,loc,timeout=None)->WebElement:
        timeout=TIMEOUT if timeout is None else TIMEOUT
        wait=WebDriverWait(self.driver,timeout=timeout)
        return wait.until(EC.element_to_be_clickable(loc))

    def fill(self,loc,info):
        self.find_element(loc).send_keys(info)

        # 封装点击操作方法

    def click(self, loc):
        self.find_element(loc).click()
        # 封装文本方法，用于断言

    def text(self, loc):
        return self.find_element(loc).text
        # 封装清除方法

    def clear(self, loc):
        self.find_element(loc).clear()



